<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Member extends CH_Controller{

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	 /**
         * Constructor
         */
	function __construct(){ 
		parent::__construct(); 
		$this->init();
		$this->load->library('encrypt');
		$this->load->model('member_model');
	} 
    	
	/**
	 * 회원 리스트
	 *
	 * @access	public
	 * @return	result
	 */
	function memberList(){
		login_admin();
		$url = $this->uri->segment(1).'/'.$this->uri->segment(2);
		$uri_array = segment_explode($this->uri->uri_string());
		$array_segment = array_keys($this->uri->segment_array(), "page");
		$page = in_array('page', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'page'))) : 1;
		$uri_segment = in_array('page', $uri_array) ? $array_segment[0]+1 : 0;
		
		$param['sch_keyname'] = urldecode($this->security->xss_clean(url_explode($uri_array, 'sch_keyname')));
		$param['sch_keyword'] = urldecode($this->security->xss_clean(url_explode($uri_array, 'sch_keyword')));
		$param['sch_gender'] = urldecode($this->security->xss_clean(url_explode($uri_array, 'sch_gender')));
		$param['sch_birth_start'] = urldecode($this->security->xss_clean(url_explode($uri_array, 'sch_birth_start')));
		$param['sch_birth_end'] = urldecode($this->security->xss_clean(url_explode($uri_array, 'sch_birth_end')));
		$param['sch_reg_start'] = urldecode($this->security->xss_clean(url_explode($uri_array, 'sch_reg_start')));
		$param['sch_reg_end'] = urldecode($this->security->xss_clean(url_explode($uri_array, 'sch_reg_end')));
		$param['page'] = $page;
		
		$sch_url_pattern = search_param_url($param);
				
		$listnum = $this->config->item('ch_listnum');	
		$count = $this->member_model->list_total($param);
		$result = $this->member_model->list_member($param);
		
		$this->load->library('pagination');
		$config['base_url'] = '/member/memberList/'.$sch_url_pattern.'/page';
		$config['total_rows'] = $count;
		$config['per_page'] = $listnum;
		$config['use_page_numbers'] = true;
		
		$config['uri_segment'] = $uri_segment;
		$this->pagination->initialize($config);
				
		$list_no = $count - $listnum * ($page-1);
		$data = array(
				'_title' => '회원리스트',
				'_group' => 'member',
				'_content' => $this->config->item('ch_manager_root').'/'.$url,
				'rst' => $result,
				'paging'=> $this->pagination->create_links(),
				'list_no'=> $list_no,
				'page'=>$page,
				'param'=>$param
				
		);
		
		return $data;
	}
	
	/**
	 * 회원 폼
	 *
	 * @access	public
	 * @return	array
	 */
	function memberForm($url=null){
		login_admin();	
		$this->load->helper('string');
		$uri_array = segment_explode($this->uri->uri_string());
		$page = in_array('page', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'page'))) : 1;
		$id = in_array('id', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'id'))) : null;
		$param['sch_keyname'] = urldecode($this->security->xss_clean(url_explode($uri_array, 'sch_keyword')));
		$param['sch_keyword'] = urldecode($this->security->xss_clean(url_explode($uri_array, 'sch_keyword')));
		$param['sch_gender'] = urldecode($this->security->xss_clean(url_explode($uri_array, 'sch_gender')));
		$param['sch_birth_start'] = urldecode($this->security->xss_clean(url_explode($uri_array, 'sch_birth_start')));
		$param['sch_birth_end'] = urldecode($this->security->xss_clean(url_explode($uri_array, 'sch_birth_end')));
		$param['sch_reg_start'] = urldecode($this->security->xss_clean(url_explode($uri_array, 'sch_reg_start')));
		$param['sch_reg_end'] = urldecode($this->security->xss_clean(url_explode($uri_array, 'sch_reg_end')));
		$param['page'] = $page;
		
		$sch_url_pattern = search_param_url($param);
		
		if ($url == null) $url =  $this->uri->segment(1).'/'.$this->uri->segment(2);
		$data = array(
				'_title' => '회원리스트',
				'_group' => 'member',
				'_content' => $this->config->item('ch_manager_root').'/'.$url,
				'link_list' => reduce_double_slashes('/member/memberList/'.$sch_url_pattern .'/page/'.$page)
		);
		//echo $this->encrypt->sha1('1234');
		$data = array_merge($data,$this->member_model->select_member($id));
		return $data ;
	}
	
	/**
	 * 회원아이디 유효성 검사
	 *
	 * @access	public
	 * @return	array
	 */
	function ajax_valid_userid(){
		$url = $this->uri_church();
		$param = array('user_id'=>$this->input->get('user_id'));
		if($this->input->get('id')) $param = array_merge($param,array('id !='=> $this->input->get('id')));
		
		$data = array(
				'_title' => '회원리스트',
				'_group' => 'member',
				'_content' => $this->config->item('ch_manager_root').'/'.$url,
				'validYN' => ($this->member_model->valid_userid_total($param)==0) ? 'Y' : 'N'
		);
		return $data;
	}
	
	/**
	 * 회원 DB 처리
	 *
	 * @access	public
	 * @return	void
	 */
	function memberDbjob(){
		//암호화
		$this->load->library('encrypt');
		$passwd = ($this->input->post('passwd')) ? $this->encrypt->sha1($this->input->post('passwd')) : null;
		
		$dbjob = $this->input->post('dbjob');
		
		$mail_id = $this->input->post('mail_id');
		$mail_domain = $this->input->post('mail_domain');
		$email = "{$mail_id}@{$mail_domain}";
		
		$tel = $this->input->post('tel');
		$tel = (sizeof($tel) > 0) ? implode('-',$tel) : null;
		
		$hp = $this->input->post('hp');
		$hp = (sizeof($hp) > 0) ? implode('-',$hp) : null;
		
		$corp_tel = $this->input->post('corp_tel');
		$corp_tel = (sizeof($corp_tel) > 0) ? implode('-',$corp_tel) : null;
		
		$corp_fax = $this->input->post('corp_fax');
		$corp_fax = (sizeof($corp_fax) > 0) ? implode('-',$corp_fax) : null;
		
		$zipcode1 = $this->input->post('zipcode1');
		$zipcode2 = $this->input->post('zipcode2');
		$post = $zipcode1.$zipcode2;
		
		$corp_zipcode1 = $this->input->post('corp_zipcode1');
		$corp_zipcode2 = $this->input->post('corp_zipcode2');
		$corp_post = $corp_zipcode1.$corp_zipcode2;
		
		$this->load->library('form_validation');
		$config = array(
				array('field'=> 'passwd','label'   => '비밀번호','rules'   => 'required|matches[chkpasswd]'),
				array('field'=> 'zipcode1','label'   => '우편번호1','rules'   => 'required'),
				array('field'=> 'zipcode2','label'   => '우편번호2','rules'   => 'required')
		);

		$this->form_validation->set_rules($config);
		if ($this->form_validation->run() == FALSE){
			$this->alert->jalert('잘못된 접근 입니다.', '','back');
			exit;
		}else{
			switch($dbjob){
				case('i'):
					$msg = '저장되었습니다.';
					$data = array(
							'admin_yn' => $this->input->post('admin_yn'),
							'membership' => $this->input->post('membership'),
							'name' => $this->input->post('name'),
							'user_id' => $this->input->post('user_id'),
							'passwd' =>  $passwd,
							'birth_date' => $this->input->post('birth_date'),
							'gender' =>  $this->input->post('gender'),
							'solar_yn' => $this->input->post('solar_yn'),
							'post' => $post,
							'addr1' => $this->input->post('addr1'),
							'addr2' => $this->input->post('addr2'),
							'tel' => $tel,
							'hp' => $hp,
							'sms_yn' => $this->input->post('sms_yn'),
							'email' => $email,
							'mailling_yn' => $this->input->post('mailling_yn'),
							'corp_name' => $this->input->post('corp_name'),
							'corp_position' => $this->input->post('corp_position'),
							'corp_tel' => $corp_tel,
							'corp_fax' => $corp_fax,
							'corp_post' => $corp_post,
							'corp_addr1' => $this->input->post('corp_addr1'),
							'corp_addr2' => $this->input->post('corp_addr2'),
							'baptism_yn' => $this->input->post('baptism_yn'),
							'church_name' => $this->input->post('church_name'),
							'church_duty' => $this->input->post('church_duty'),
							'introducer' => $this->input->post('introducer'),
							'join_path' => $this->input->post('join_path'),
							'reg_date' =>  date("Y-m-d H:i:s")
					);
					break;
				case('u'):
					$msg = '수정되었습니다.';
					$data = array(
							'admin_yn' => $this->input->post('admin_yn'),
							'membership' => $this->input->post('membership'),
							'name' => $this->input->post('name'),
							'passwd' =>  $passwd,
							'birth_date' => $this->input->post('birth_date'),
							'gender' =>  $this->input->post('gender'),
							'solar_yn' => $this->input->post('solar_yn'),
							'post' => $post,
							'addr1' => $this->input->post('addr1'),
							'addr2' => $this->input->post('addr2'),
							'tel' => $tel,
							'hp' => $hp,
							'sms_yn' => $this->input->post('sms_yn'),
							'email' => $email,
							'mailling_yn' => $this->input->post('mailling_yn'),
							'corp_name' => $this->input->post('corp_name'),
							'corp_position' => $this->input->post('corp_position'),
							'corp_tel' => $corp_tel,
							'corp_fax' => $corp_fax,
							'corp_post' => $corp_post,
							'corp_addr1' => $this->input->post('corp_addr1'),
							'corp_addr2' => $this->input->post('corp_addr2'),
							'baptism_yn' => $this->input->post('baptism_yn'),
							'church_name' => $this->input->post('church_name'),
							'church_duty' => $this->input->post('church_duty'),
							'introducer' => $this->input->post('introducer'),
							'join_path' => $this->input->post('join_path'),
							'mod_date' =>  date("Y-m-d H:i:s")
					);
					break;
				case('d'):
					$msg = '삭제 되었습니다.';
					$data = array('del_yn'=>'Y');
					break;
			
			}
			$this->member_model->member_dbjob($data);
			$this->alert->jalert($msg, base_url('/member/memberList'));
		}
	}
	
	/**
	 * 회원 가입폼 설정
	 *
	 * @access	public
	 * @return	result
	 */
	function settingForm(){
		login_admin();
		$url = $this->uri_church();
		$data = array(
				'_title' => '회원가입폼관리',
				'_group' => 'member',
				'_content' => $this->config->item('ch_manager_root').'/'.$url
		);
		$data = array_merge($data,$this->member_model->select_member_terms());
		return $data ;
	}
	
	/**
	 * 회원 약관 설정
	 *
	 * @access	public
	 * @return	result
	 */
	function settingFormDbjob(){
		login_admin();
		$dbjob = $this->input->post('dbjob');
		switch($dbjob){
			case('i'):
				$msg = '등록 되었습니다.';
				$data = array('info_use'=> $this->input->post('info_use'),'info_personal'=> $this->input->post('info_personal'));
			break;
			case('u'):
				$msg = '수정 되었습니다.';
				$data = array('info_use'=> $this->input->post('info_use'),'info_personal'=> $this->input->post('info_personal'));
			break;
		}
		$this->member_model->member_term_dbjob($data);
		$this->alert->jalert($msg, base_url('/member/settingForm'));
	}
	/**
	 * 탈퇴 회원 리스트
	 *
	 * @access	public
	 * @return	result
	 */
	function secedeList(){
		login_admin();
		$url = $this->uri->segment(1).'/'.$this->uri->segment(2);
		$uri_array = segment_explode($this->uri->uri_string());
		$array_segment = array_keys($this->uri->segment_array(), "page");
		$page = in_array('page', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'page'))) : 1;
		$uri_segment = in_array('page', $uri_array) ? $array_segment[0]+1 : 0;
		$param['sch_reg_start'] = urldecode($this->security->xss_clean(url_explode($uri_array, 'sch_reg_start')));
		$param['sch_reg_end'] = urldecode($this->security->xss_clean(url_explode($uri_array, 'sch_reg_end')));
		$param['page'] = $page;
		$sch_url_pattern = search_param_url($param);
				
		$listnum = $this->config->item('ch_listnum');	
		$count = $this->member_model->list_secede_total($param);
		$today = $this->member_model->list_secede_today();
		$result = $this->member_model->list_secede_member($param);
		
		$this->load->library('pagination');
		$config['base_url'] = '/member/secedeList/'.$sch_url_pattern.'/page';
		$config['total_rows'] = $count;
		$config['per_page'] = $listnum;
		$config['use_page_numbers'] = true;
		
		$config['uri_segment'] = $uri_segment;
		$this->pagination->initialize($config);
				
		$list_no = $count - $listnum * ($page-1);

		$data = array(
				'_title' => '회원탈퇴',
				'_group' => 'member',
				'_content' => $this->config->item('ch_manager_root').'/'.$url,
				'rst' => $result,
				'paging'=> $this->pagination->create_links(),
				'count'=> $count,
				'today'=> $today,
				'list_no'=> $list_no,
				'page'=>$page,
				'param'=>$param
				
		);
		
		return $data ;
	}
}

/* End of file html.php */
/* Location: ./application/controllers/member.php */